#include "StdAfx.h"
#include "AiObjectBase.h"

using namespace Adaptive::Foundation;

/*--------------------------------------------------------------------------------
 * Constructor / Dispose / Destructor Methods
 *------------------------------------------------------------------------------*/
#pragma region Constructor / Dispose / Destructor Methods
// Initializes a new instance of the class.
AiObjectBase::AiObjectBase()
{
	_name = String::Empty;
	_cleared = false;
	_disposed = false;
}
// Class destructor.
AiObjectBase::~AiObjectBase()
{
	if (!_disposed)
	{
		OnDisposing(EventArgs::Empty);
		_name = String::Empty;
		_cleared = true;
		_disposed = true;
	}
}
// Dispose.
AiObjectBase::!AiObjectBase()
{
	_name = nullptr;
	_cleared = true;
	_disposed = true;
}
#pragma endregion

/*--------------------------------------------------------------------------------
 * Public Properties
 *------------------------------------------------------------------------------*/
#pragma region Public Properties
// Gets a value indicating whether the object has been disposed.
bool AiObjectBase::Cleared::get()
{
	return _cleared;
}
// Gets a value indicating whether the object has been disposed.
bool AiObjectBase::Disposed::get()
{
    return _disposed;
}
// Gets a value indicating whether the object has been disposed.
String^ AiObjectBase::Name::get()
{
	return _name;
}
void AiObjectBase::Name::set(String^ value)
{
	if (value != _name)
	{
		_name = value;
		OnNameChanged(EventArgs::Empty);
	}
}
#pragma endregion

/*--------------------------------------------------------------------------------
 * Protected Methods / Event Methods
 *------------------------------------------------------------------------------*/
#pragma region Protected Methods / Event Methods
// Raises the <see cref="Clearing"/> event.
void AiObjectBase::OnClearing(EventArgs^ e)
{
	Clearing(this, e);
}
void AiObjectBase::SetDirty()
{
	_cleared = false;
}
// Raises the <see cref="Disposing"/> event.
void AiObjectBase::OnDisposing(EventArgs^ e)
{
	Disposing(this, e);
}
// Raises the <see cref="OnNameChanged"/> event.
void AiObjectBase::OnNameChanged(EventArgs^ e)
{
	NameChanged(this, e);
}
array<Byte>^ AiObjectBase::UnicodeToAscii(String^ originalUnicodeString) 
{
	array<Byte>^ data = nullptr;
	array<Byte>^ returnValue = nullptr;
	int length = 0;
	int count = 0;
	int pcount = 0;

	data = System::Text::Encoding::Unicode->GetBytes(originalUnicodeString);
	length = data->Length;
	returnValue = gcnew array<Byte>((int)((float)length / 2));
	for(count = 0; count < length; count+=2)
	{
		returnValue[pcount] = data[count];
		pcount++;
	}
	delete(data);
	return returnValue;
}
String^ AiObjectBase::AsciiToUnicode(array<Byte>^ originalAsciiData) 
{
	return System::Text::Encoding::ASCII->GetString(originalAsciiData);
}
#pragma endregion

/*--------------------------------------------------------------------------------
 * Public Methods / Event Methods
 *------------------------------------------------------------------------------*/
#pragma region Protected Methods / Event Methods
// Clears the contents of the object instance without disposing of the instance.
void AiObjectBase::Clear()
{
	_cleared = true;
}
#pragma endregion
